<?php

namespace App\Models;

use App\Admin\Traits\AdminBuilder;
use App\Admin\Traits\ModelTree;
use App\Admin\Traits\CanValidate;
use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    use CanValidate;
    use ModelTree;
    use AdminBuilder;

    protected $fillable = ['parent_id', 'title', 'icon', 'uri', 'order'];

    public function validateRules()
    {
        $valid_id = Menu::where('id', '<>', (int)$this->id)
                        ->pluck('id')
                        ->toArray();
        $valid_id = implode(',', array_merge($valid_id, [0]));

        return [
            'parent_id' => 'required|in:' . $valid_id,
            'title'     => 'required|max:50',
            'icon'      => 'max:50',
            'uri'       => 'max:255',
        ];
    }

    public function validateAttributes()
    {
        return [
            'parent_id' => '父级菜单',
            'title'     => '菜单名称',
            'icon'      => '菜单图标',
            'uri'       => '菜单地址',
        ];
    }
}
